package com.lhy.ltss.dao;

import com.lhy.ltss.entity.MainOrder;
import com.lhy.ltss.entity.Order;
import org.apache.ibatis.annotations.*;

import javax.websocket.server.ServerEndpoint;
import java.util.List;

/**
 * @description: 订单的持久化操作
 * @author: 李汇远
 */
@Mapper
public interface OrderDao {

    // 登记订单详情
    @Update("update order_detail set status='配送中' where `order`=#{order} and status='登记中'")
    int registerOrderDetail(Integer order);

    // 确认配送成功的订单详情
    @Update("update order_detail set status='已完成' where `order`=#{order} and status='配送中'")
    int distributeOrderDetail(Integer order);

    // 更新订单详情的状态
    @Update("update order_detail set status=#{status} where id=#{id}")
    int updateOrderDetailStatus(@Param("status") String status, @Param("id") Integer id);

    // 更新订单状态
    @Update("update `order` set status=#{status} where id=#{id}")
    int updateOrderStatus(@Param("status") String status, @Param("id") Integer id);

    // 如果订单详情状态为 ('登记中','已取消') 则修改订购状态，
    // 并且存在登记中的状态则修改为登记中，否认为已取消，

    // 获取主订单的id
    @Select("select `order` from order_detail where id=#{id}")
    int getParentOrderId(Integer id);

    // 判断订单下的订单详情是否全部为 ('登记中','已取消')
    @Select("select count(*) from order_detail where `order`=#{order} and status not in ('登记中','已取消')")
    int getNoFinishOrderDetailNum(Integer order);

    // 获取订单下登记中的订单详情数目
    @Select("select count(*) from order_detail where `order`=#{order} and status='登记中'")
    int getRegisterOfOrderNum(Integer order);


    // 查询教师的历史订单
    @Select("select * from order_detail  where `order` in (select id from `order` where username=#{username} ) and status in ('已完成','已取消') limit #{page},10")
    List<Order> selectHistoryByUsername(@Param("username") String username, @Param("page") Integer page);

    // 统计教师历史订单的总数
    @Select("select count(*) from order_detail  where `order` in (select id from `order` where username=#{username} ) and status in ('已完成','已取消')")
    int countHistory(@Param("username") String username);

    // 查询教师的未完成订单，分页
    @Select("select * from order_detail  where `order` in (select id from `order` where username=#{username} ) and status not in ('已完成','已取消') limit #{page},10")
    List<Order> selectTodoByUsername(@Param("username") String username, @Param("page") Integer page);

    // 统计教师未完成订单的总数
    @Select("select count(*) from order_detail  where `order` in (select id from `order` where username=#{username} ) and status not in ('已完成','已取消')")
    int countTodo(@Param("username") String username);

    // 统计教师的指定状态订单数目
    @Select("select count(*) from order_detail  where `order` in (select id from `order` where username=#{username} ) and status=#{status}")
    int count(@Param("username") String username, @Param("status") String status);

    // 统计主订单数目，依据状态范围
    @Select("select count(*) from `order` where status in (${status}) ")
    int countOrderByStatusRange(String status);

    // 添加一个订单，并返回插入的主键
    @Insert("insert into `order`(username,status,`create`,address) values(#{username},#{status},#{create},#{address})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Integer.class)
    int addMainOrder(MainOrder mainOrder);

    // 添加一个订单详情
    @Insert("insert into `order_detail`(`order`,book_index,book_name,book_publish,book_editor,status) " +
            "values(#{order},#{book_index},#{book_name},#{book_publish},#{book_editor},#{status})")
    int addOrder(Order order);

    // 查询指定用户名和状态的订单
    @Select("select * from `order` where username like #{username} and status in ${status} limit #{start},10")
    List<MainOrder> findOrderByUsernameAndStatus(@Param("username") String username, @Param("status") String status, @Param("start") Integer start);

    @Select("select count(*) from `order` where username like #{username} and status in ${status} ")
    int countByUsernameAndStatus(@Param("username") String username, @Param("status") String status);

    // 查询订单订单的订单详情
    @Select("select * from order_detail where `order`=#{orderId}")
    List<Order> findOrderDetailByOrderId(Integer orderId);

    // 查询找书员的订单详情
    @Select("select * from order_detail where status='找书中' and book_index like #{bookIndex} and id in (select order_detail from find where username=#{username})")
    List<Order> getFinderOrder(@Param("bookIndex") String bookIndex, @Param("username") String username);

}
